package com.huluobo.day11_im.contact.view;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.huluobo.day11_im.R;
import com.huluobo.day11_im.adapter.MyContactListAdapter;
import com.huluobo.day11_im.adapter.MyFragmentAdapter;
import com.huluobo.day11_im.bean.ContactListItemBean;
import com.huluobo.day11_im.contact.prensenter.ContactContract;
import com.huluobo.day11_im.contact.prensenter.ContactPresenter;
import com.huluobo.day11_im.weight.SlideBar;

import java.util.ArrayList;
import java.util.List;

public class MyContactFragment extends Fragment implements ContactContract.View {
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;

    private ContactPresenter contactPresenter;
    private MyContactListAdapter myContactListAdapter;
    private List<ContactListItemBean> contactListItemBeans = new ArrayList<>();

    private SlideBar slideBar;
    private TextView section;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_my_contact, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        swipeRefreshLayout = view.findViewById(R.id.contact_SwipeRefreshLayout);
        recyclerView = view.findViewById(R.id.contact_recyclerView);

        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.purple_200));
        swipeRefreshLayout.setRefreshing(true);
        section = view.findViewById(R.id.section);

        slideBar = view.findViewById(R.id.contact_slideBar);
        slideBar.setOnSectionChangeListener(new SlideBar.OnSectionChangeListener() {
            @Override
            public void onSectionChange(String firstLetter) {
                section.setVisibility(View.VISIBLE);
                section.setText(firstLetter);
                moveToLetterPosition(firstLetter);
            }

            @Override
            public void onSlideFinish() {
                section.setVisibility(View.GONE);
            }
        });


        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                contactPresenter.loadContacts();
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        contactPresenter = new ContactPresenter(this);

        contactPresenter.loadContacts();
//        myContactListAdapter = new MyContactListAdapter(contactListItemBeans, getContext());
//        recyclerView.setAdapter(myContactListAdapter);
    }

    @Override
    public void loadContactsSuccess(List<ContactListItemBean> usernames) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
                contactListItemBeans = usernames;
                myContactListAdapter = new MyContactListAdapter(contactListItemBeans, getContext());
                recyclerView.setAdapter(myContactListAdapter);
                myContactListAdapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    public void loadContactsFailed(String error) {
        swipeRefreshLayout.setRefreshing(false);
        Log.i("TagA", "error:" + error);
    }

    private void moveToLetterPosition(String firstLetter) {
        for (int i = 0; i < contactListItemBeans.size(); i++) {
            if ((contactListItemBeans.get(i).getFirstLetter() + "").equals(firstLetter)) {
                recyclerView.smoothScrollToPosition(i);//smooth 平滑移动到某个位置
            }
        }
    }
}
